/**
 * load-more 上拉加载
 * @description 该混入js用于上拉加载下一页，及加载，结束，空数据的控制
 * items { Array } 数据列表
 * pages { Object } 页数
 * loading { Boolean } 加载状态
 * finished { Boolean } 结束状态
 * isEmpty { Boolean }  是否空数据
 * awaitLoading  { Boolean }  执行等待
 * loadingStatus { String }  加载效果判断
 */
export default {
  data() {
    return {
      items: [],
      pages: {
        perPage: 15,
        currentPage: 1,
        hasNext: true,
      },
      loading: true,
      finished: false,
      isEmpty: false,
      awaitLoading: false, // 执行等待，必须数据请求完，执行下次动作
    };
  },
  computed: {
    loadingStatus() {
      console.log('page--111',JSON.stringify(this.pages));
      if (this.isEmpty) {
        return '';
      }
      if (this.loading && !this.finished) {
        return 'loading';
      }
      if (this.finished) {
        return 'nomore';
      }
      return 'loadmore';
    },
  },
  onReachBottom() {
    // 触碰到底部——系统全屏默认触底
    this.loading && !this.finished && this.loadMore();
  },
  methods: {
    onReachBottom() {
      // 触碰到底部——scroll-view触底调用
      this.loading && !this.finished && this.loadMore();
    },
    async resetInit(params) {
      // 初始化
      this.resetData();
      console.log('params--222',JSON.stringify(params));
      const page = await this.initData(params);

      console.log('page--222',JSON.stringify(page));

      this.setPages(page);
    },
    isFinished() {
      this.finished = true;
      this.loading = false;
    },
    async loadMore() {
      if (this.awaitLoading) {
        return;
      }
      this.awaitLoading = true;
      if (this.pages.hasNext) {
        this.pages.currentPage += 1;
        const page = await this.initData();
        this.pages.hasNext = page.hasNext;
        if (page.hasNext) {
          this.nextPage();
        } else {
          this.isFinished();
        }
      } else {
        this.isFinished();
      }

      this.awaitLoading = false;
    },
    nextPage() {
      

      // 不满一页,强制下一页,若hasNext
      if (this.pages.hasNext) {
        this.loading && !this.finished && this.loadMore();
      }
    },
    setPages(page = {}) {
      this.isEmpty = page.empty;
      if (this.isEmpty) {
        return;
      }
      this.pages.hasNext = page.hasNext;
      if (!page.hasNext) {
        // 不满一页
        this.isFinished();
      } else {
        // 下一页
        //  this.nextPage();
      }
      
    },
    resetData() {
      this.items = [];
      this.pages = {
        perPage: 15,
        currentPage: 1,
        hasNext: true,
      };
      this.loading = true;
      this.finished = false;
      this.isEmpty = false;
    },
  },
};